package com.anlgin.anl_tea.controller;

import com.alibaba.fastjson.JSON;
import com.anlgin.anl_tea.entities.*;
import com.anlgin.anl_tea.tools.CommodityTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.anlgin.anl_tea.service.ICommodityService;

import java.util.LinkedHashMap;
import java.util.List;

@RestController
@RequestMapping("/commodity")
@CrossOrigin
public class CommodityController {
    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private ICommodityService commodityService;

    @Autowired
    private CommodityTools tools;

    @RequestMapping("/getCommodityInfo")
    public RetResult getCommodityInfo() {
        logger.info("getCommodityInfo。。。");
        List<Commodity> commodities = commodityService.getCommodities();
        List<CommodityType> types = commodityService.getAllCommodityTypes();
        LinkedHashMap map = tools.formatCommodity(types, commodities);
        System.out.println(map);
        return RetResponse.makeOKRsp(map);
    }

    @RequestMapping("/getAttributeByIds")
    public RetResult getAttributeByIds(String attributeIds) {
        logger.info("getAttributeByIds。。。");
        System.out.println(attributeIds);
        List<Integer> ids = JSON.parseArray(attributeIds, Integer.class);
        LinkedHashMap<String, List<Attribute>> map = new LinkedHashMap<>();
        for (Integer id : ids) {
            List<Attribute> attributes = commodityService.getAttributeById(id);
            map.put(attributes.get(0).getAttributeName(),attributes);
        }
        return RetResponse.makeOKRsp(map);
    }
}
